var express = require('express');
var router = express.Router();
var { goodModel } =require("../model/goods");
var { shopcarModel } =require("../model/shopcar");

/* GET home page. */
router.get("/goodlist",async(req,res)=>{
  let data=await goodModel.find()

  res.send(data)
})

router.get("/shopcar",async(req,res)=>{
    let user_id = req.query.user_id
    let data = await shopcarModel.find({userid:user_id})
    res.send(data)
})

router.post("/checkall",async(req,res)=>{
    let reqinfo = req.body
    let user_id = reqinfo.user_id
    let checked = reqinfo.checked
    await shopcarModel.updateMany({userid:user_id},{ischecked:checked})
    res.send({
        "code":200
    })
})

router.post("/checkone",async(req,res)=>{
    let reqinfo = req.body
    let user_id = reqinfo.user_id
    let checked = reqinfo.checked
    await shopcarModel.updateOne({_id:user_id},{ischecked:checked})
    res.send({
        "code":200
    })
})

router.post("/changenum",async(req,res)=>{
    let reqinfo = req.body
    let user_id = reqinfo.user_id
    let num = reqinfo.num
    await shopcarModel.updateOne({_id:user_id},{num:num})
    res.send({
        "code":200
    })
})

router.get("/orderlist",async(req,res)=>{
    let user_id = req.query.user_id
    let flag = req.query.flag
    let data = await shopcarModel.find({userid:user_id})
    console.log(flag);
    if(flag == 'all'){
        data=data
    }
    res.send(data)
})

module.exports = router;
